package cn.charmaine.pet.controller;

import cn.charmaine.basic.Exception.CustomException;
import cn.charmaine.basic.util.LoginContext;
import cn.charmaine.org.domain.Department;
import cn.charmaine.org.domain.Employee;
import cn.charmaine.org.domain.Logininfo;
import cn.charmaine.org.mapper.IEmployeeMapper;
import cn.charmaine.pet.domain.PetDetail;
import cn.charmaine.pet.domain.PetType;
import cn.charmaine.pet.mapper.PetDetailMapper;
import cn.charmaine.pet.service.IPetDetailService;
import cn.charmaine.pet.service.IPetService;
import cn.charmaine.pet.domain.Pet;
import cn.charmaine.pet.query.PetQuery;
import cn.charmaine.basic.util.AjaxResult;
import cn.charmaine.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.Detail;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;

    @Autowired
    private IEmployeeMapper employeeMapper;

    @Autowired
    private IPetDetailService petDetailService;

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet , HttpServletRequest request){
        //利用工具类解析，得到logininfo
        Logininfo logininfo = LoginContext.getCurrentUser(request);
        //利用logininfo的id查询对应的employee
        Employee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        //是否为空
        if(employee != null && employee.getShopId() != null){
            //给宠物设置店铺id
            pet.setShopId(employee.getShopId());
        }
        try {
            if( pet.getId()!=null){
                petService.update(pet);
            }else{
                petService.add(pet);
            }
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.deleteById(id);
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        Pet pet = petService.queryById(id);
        System.out.println(pet);
        return petService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){
        return petService.queryAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.queryData(query);
    }

    /**
     * 通过pet的id查询PetDetail
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public PetDetail detail(@PathVariable("id") Long id){
        return petDetailService.queryByPetId(id);
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/dels")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try{
            petService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 宠物上架审核
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids,HttpServletRequest request){
        try {
            petService.auditOnsale(ids,request);
            return AjaxResult.thisSuccess();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.thisSuccess().setSuccess(false).setMessage("上架失败");
    }

    /**
     * 宠物下架审核
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try{
            Date date = new Date();
            petService.auditOffsale(ids,date);
            return AjaxResult.thisSuccess();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.thisSuccess().setSuccess(false).setMessage("上架失败");
    }

    /**
     * 店铺数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/shopIndex")
    public PageList<Pet> shopIndex(@RequestBody PetQuery query)
    {
        return petService.queryShopIndex(query);
    }

}
